package name.cyrax.red.jpv.impl.funcs.core;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public class FunctorInstanceTools
{
//	public static FunctorInstance newInstance(Functor functor, 
//			Object methodObject, String methodName, Class<?> returnType, Class<?> param0) 
//	{
//		return new FunctorInstanceTools(functor, 
//				getMethodHandle(methodObject, methodName, returnType, param0));
//	}
//
//	public static FunctorInstance newInstance(Functor functor, 
//			Object methodObject, String methodName, Class<?> returnType, Class<?> param0, Class<?> param1) 
//	{
//		return new FunctorInstanceTools(functor,
//				getMethodHandle(methodObject, methodName, returnType, param0, param0));
//	}
//
//	public static FunctorInstance newInstance(Functor functor,
//			Object methodObject, String methodName, MethodType mt)
//	{
//		return new FunctorInstanceTools(functor, getMethodHandle(methodObject, methodName, mt));
//	}
//	
//	public static FunctorInstance newInstance(Functor functor,
//			Object methodObject, MethodHandle methodHandle)
//	{
//		return new FunctorInstanceTools(functor, methodHandle.bindTo(methodObject));
//	}
	
	
	public static MethodHandle getMethodHandle(Object methodObject, 
			String methodName, Class<?> returnType, Class<?> param0)
	{
		return getMethodHandle(methodObject, methodName, MethodType.methodType(returnType, param0));
	}

	public static MethodHandle getMethodHandle(Object methodObject, 
			String methodName, Class<?> returnType, Class<?> param0, Class<?> param1)
	{
		return getMethodHandle(methodObject, methodName, MethodType.methodType(returnType, param0, param1));
	}

	public static MethodHandle getMethodHandle(Object methodObject, String methodName, MethodType mt)
	{
		try {
			MethodHandle mh = MethodHandles.lookup().findVirtual(methodObject.getClass(), methodName, mt);
			return mh.bindTo(methodObject);
		} catch (NoSuchMethodException | IllegalAccessException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static MethodHandle getMethodHandle(Class<?> methodClass, String methodName, MethodType mt)
	{
		try {
			return MethodHandles.lookup().findVirtual(methodClass, methodName, mt);
		} catch (NoSuchMethodException | IllegalAccessException e) {
			throw new RuntimeException(e);
		}
	}
}
